﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Drawing;
using System.Data;

namespace MNNUTEM
{
    class crudInventario
    {

        private Conexion cn = new Conexion();
        String query;
        private mensaje msj;

        public void Insert(TextBox id, TextBox Q, TextBox D, TextBox A, TextBox C, TextBox T, TextBox F, TextBox rop, string tipoInventario, int idProducto, int idSucursal)
        {
            try
            {
                query = "insert into Inventario (idInventario,QInventario,DInventario,AInventario,CInventario,TInventario,FInventaio,tipoInventario, rop, Producto_idProducto,Producto_Sucursal_idSucursal) values ('" + id.Text + "','" + Q.Text + "','" + D.Text + "','" + A.Text + "','" + C.Text + "','" + T.Text + "','" + F.Text + "','" + tipoInventario + "', '"+rop.Text+"','" + idProducto + "','" + idSucursal + "')";
                MySqlCommand myCommand = new MySqlCommand(query, cn.conn());
              
                myCommand.ExecuteNonQuery();
                cn.conn().Close();
                msj = new mensaje("Acción realizada con éxito.", 1300, Color.Green);

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw new Exception("Se ha producido un error:", ex);
            }



        }
        public void Delete(int ID)
        {

            try
            {
                query = "delete from Inventario where idInventario='" + ID + "'";
                MySqlCommand myCommand = new MySqlCommand(query, cn.conn());
               
                myCommand.ExecuteNonQuery();
                cn.conn().Close();
                msj = new mensaje("Acción realizada con éxito.", 1300, Color.Green);

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw new Exception("Se ha producido un error:", ex);
            }

        }

        public void SetId(int ID, int idsuperior)
        {


            try
            {
                query = "UPDATE  Costo set idCosto='" + ID + "' where idCosto='" + idsuperior + "'";
                MySqlCommand myCommand = new MySqlCommand(query, cn.conn());
           
                myCommand.ExecuteNonQuery();
                cn.conn().Close();


            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw new Exception("Se ha producido un error:", ex);
            }


        }


        private void Set()
        { }


        public DataTable Consultation(string consultation)
        {

            try
            {


                query = consultation;
                MySqlCommand myCommand = new MySqlCommand(query, cn.conn());
                MySqlDataAdapter sda = new MySqlDataAdapter();
                sda.SelectCommand = myCommand;
                DataTable dtable = new DataTable();
                sda.Fill(dtable);


                return dtable;

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw new Exception("Se ha producido un error:", ex);
            }

        }

     



        public void Consultation3(string consultation, DataGridView dataGridView1)
        {

DataTable schemaTable; 
MySqlDataReader myReader; 

MySqlCommand cmd= new MySqlCommand(query, cn.conn());
cmd.Connection = cn.conn();
cmd.CommandText = "SELECT  if (rop< cantidadProducto, 'yes' , 'not') FROM Inventario JOIN Producto WHERE Producto_idProducto = idProducto";
myReader = cmd.ExecuteReader();
schemaTable = myReader.GetSchemaTable();
int x = 0;
while (myReader.Read())
{
    
  
    dataGridView1.Rows[x].DefaultCellStyle.BackColor = Color.Pink;
    x++;
}
         
            
               
        }


        public void Consultation3(DataGridView dataGridView1)
        {

            DataTable schemaTable;
            MySqlDataReader myReader;

            MySqlCommand cmd = new MySqlCommand(query, cn.conn());
            cmd.Connection = cn.conn();
            cmd.CommandText = "SELECT *from Producto ";
            myReader = cmd.ExecuteReader();
            schemaTable = myReader.GetSchemaTable();
            int x = 0, y = 0;
            while (myReader.Read())
            {
                if (int.Parse(myReader.GetString(8)) <= 10)
                {

                    dataGridView1.Rows[x].DefaultCellStyle.BackColor = Color.Pink;
                    x++;

                }
                else
                {
                    x++;
                   
                }

            }



        }



        public DataSet Consultation2(string consultation)
        {

            try
            {


                query = consultation;
                MySqlCommand myCommand = new MySqlCommand(query, cn.conn());
                MySqlDataAdapter sda = new MySqlDataAdapter();
                sda.SelectCommand = myCommand;
                DataSet DS = new DataSet();
                sda.Fill(DS);


                return DS;

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw new Exception("Se ha producido un error:", ex);
            }

        }



    }
}
